﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;


/// <summary>
/// Summary description for ThanhVien
/// </summary>
public class ThanhVien
{
    private string _TenDN;

    public string TenDN
    {
        get { return _TenDN; }
        set { _TenDN = value; }
    }
    private string _MK;

    public string MK
    {
        get { return _MK; }
        set { _MK = value; }
    }
    private string _Email;

    public string Email
    {
        get { return _Email; }
        set { _Email = value; }
    }
    private string _HoTen;

    public string HoTen
    {
        get { return _HoTen; }
        set { _HoTen = value; }
    }
    private string _DiaChi;

    public string DiaChi
    {
        get { return _DiaChi; }
        set { _DiaChi = value; }
    }
    private string _SDT;

    public string SDT
    {
        get { return _SDT; }
        set { _SDT = value; }
    }
    private int _loai;

    public int Loai
    {
        get { return _loai; }
        set { _loai = value; }
    }

    public ThanhVien()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public bool ThemThanhVien()
    {
        try
        {

            Ketnoi.Open();
            string sQuery = "insert into THANHVIEN(TENDN,MATKHAU,HOTEN,DIACHI,SDT,EMAIL) values('" + _TenDN + "','" + _MK + "','" + _HoTen + "','" + _DiaChi + "','" + _SDT + "','" + _Email + "')";
            OleDbCommand cmd = new OleDbCommand(sQuery, Ketnoi.Connect);
            int result = cmd.ExecuteNonQuery();
            Ketnoi.Close();
            if (result > 0)
                return true;
            else
                return false;

        }
        catch
        {
            
            Ketnoi.Close();
            return false;
        }
    }
    public ThanhVien(String _TenDN, string _MK)
    {
        TenDN = _TenDN;
        MK = _MK;
    }
    public DataTable LayDoiThongTin(string sTen)
    {
        try
        {

            string sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|BANHANG.mdb";
            OleDbConnection con = new OleDbConnection(sCon);
            con.Open();

            string sQuery = "select TenDN,HoTen,DiaChi,SDT from THANHVIEN where TenDN = '" + sTen + "'";
            OleDbDataAdapter da = new OleDbDataAdapter(sQuery, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            con.Close();
            return dt;

        }
        catch
        {
            return null;
        }
    }
    public bool CapNhat(ObjectUpdate obj)
    {
        try
        {
            Ketnoi.Open();
            string sQuery = "UPDATE THANHVIEN SET ";
            for (int i = 0; i < obj.Item.Count-1; i++)
            {
                sQuery += obj.Item[i].key + "='" + obj.Item[i].value + "', ";
            }
            sQuery += obj.Item[obj.Item.Count - 1].key + "='" + obj.Item[obj.Item.Count - 1].value + "' WHERE TENDN='" + _TenDN + "'";
            OleDbCommand cm = new OleDbCommand(sQuery, Ketnoi.Connect);
            bool result = cm.ExecuteNonQuery() == 0 ? false : true;
            Ketnoi.Close();
            return result;
        }
        catch 
        {
            
            return false;
        }
    }
    public bool DoiThongTin(string sHoTen, string sDiaChi, string sSDT)
    {
        try
        {

            string sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|BANHANG.mdb";
            OleDbConnection con = new OleDbConnection(sCon);
            con.Open();

            string sQuery = "update THANHVIEN set HoTen='" + sHoTen + "', DiaChi='" + sDiaChi + "', SDT='" + sSDT + "' where TenDN='" + _TenDN + "'";
            OleDbCommand cmd = new OleDbCommand(sQuery, con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result > 0)
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }

    }
   public static DataTable getFullTV(string ten)
   {
       Ketnoi.Open();
       string sQuery = "SELECT TENDN, HOTEN, DIACHI, SDT, EMAIL, XOA, LOAITV FROM THANHVIEN WHERE TENDN = '" + ten + "'";
       OleDbDataAdapter da = new OleDbDataAdapter(sQuery, Ketnoi.Connect);
       DataTable dt = new DataTable();
       da.Fill(dt);
       Ketnoi.Close();
       return dt;
   }
   public bool checkUserName(string sten)
{
    string sCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|BANHANG.mdb";
    OleDbConnection con = new OleDbConnection(sCon);
    string sQuery = "select TenDN,HoTen,DiaChi,SDT from THANHVIEN where TenDN = '" + sten + "'";
    OleDbCommand cmd = new OleDbCommand(sQuery, con);
    OleDbParameter op = new OleDbParameter();
    op=cmd.Parameters.AddWithValue(sten,TenDN);
    OleDbDataReader dtr;
    
    con.Open();
    dtr = cmd.ExecuteReader();
    if (dtr.Read())
    {

        con.Close();
        return true;
    }
    else
    {
        con.Close();
        return false;
    }
}


   
}
